#!/usr/bin/env python
# coding: utf-8

from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import  *
from data import *
from pycalendar import *
from staff import *
import sys
import datetime


class Regist(QWidget):

	def __init__(self):
		super(Regist, self).__init__()
		self.setFixedSize(300, 200)
		self.setWindowTitle("登记")
		self.set_ui()

	def set_ui(self):
		self.date = QDateEdit()
		self.date_choose = QPushButton("日期")
		self.date_choose.clicked.connect(self.choose_date)
		self.staffs = QLineEdit()
		self.staffs_choose = QPushButton("员工选择")
		self.staffs_choose.clicked.connect(self.choose_staffs)

		self.confirm_bnt = QPushButton("确定")
		self.confirm_bnt.clicked.connect(self.save_record)
		self.close_bnt = QPushButton("关闭")
		self.close_bnt.clicked.connect(lambda:self.close())
		self.query_bnt = QPushButton("查询")
		self.query_bnt.clicked.connect(self.query)

		self.layout = QGridLayout()
		self.layout.addWidget(self.date, 0, 0, 1, 2)
		self.layout.addWidget(self.date_choose, 0, 2)
		self.layout.addWidget(self.staffs, 1, 0, 1, 2)
		self.layout.addWidget(self.staffs_choose,1,2)
		self.layout.addWidget(self.confirm_bnt,2,2)
		self.layout.addWidget(self.close_bnt,2,0)
		self.layout.addWidget(self.query_bnt,2,1)

		self.setLayout(self.layout)

	def choose_staffs(self):
		self.new_window = Staff()
		self.new_window.show()
		self.new_window.staff_signal.connect(self.insert_staff)

	def insert_staff(self, rslt):
		rslt2 = ','.join(list(rslt))
		self.staffs.setText(rslt2)

	def choose_date(self):
		self.new_window = Calendar()
		self.new_window.show()
		self.new_window.date_signal.connect(self.insert_date)

	def insert_date(self, date):
		self.date.setDate(date)

	def change_date(self,pyqtdate): 
		pydate = pyqtdate.date().toPyDate()
		base_date = datetime.date(1970,1,1)
		day_delta = pydate - base_date
		days = day_delta.days
		seconds = days*24*3600
		return seconds

	def  save_record(self):
		db = DB()
		cur = db.cur
		if self.staffs.text() != '':
			rslt_list = list(self.staffs.text().split(','))
			for i in rslt_list:
				sql = 'insert into zhiban values ({}, "{}")'.format(self.change_date(self.date), i)
				cur.execute(sql)
		msg = QMessageBox.information(self, 'tips','是否保存',QMessageBox.Yes, QMessageBox.Yes)
		if msg == QMessageBox.Yes:
			db.con.commit()
		else:
			pass
		db.con.close()

	def query(self):
		self.window = Query()
		self.window.show()

class Query(QWidget):

	def __init__(self):
		super(Query, self).__init__()
		self.setFixedSize(300, 400)
		self.setWindowTitle("查询")
		self.set_ui()

	def set_ui(self):
		self.begin_date = QDateEdit()
		self.begin_date.setDate(QDate.currentDate())
		self.end_date = QDateEdit()
		self.end_date.setDate(QDate.currentDate())
		begin_date_choose = QPushButton("<")
		end_date_choose = QPushButton(">")
		begin_date_choose.clicked.connect(self.begin_date_cal)
		end_date_choose.clicked.connect(self.end_date_cal)
		self.txt = QTextEdit()

		query_bnt = QPushButton("查询")
		query_bnt.clicked.connect(self.query)
		close_bnt = QPushButton("关闭")
		close_bnt.clicked.connect(lambda:self.close())

		self.layout = QGridLayout()
		self.layout.addWidget(self.begin_date,0,0)
		self.layout.addWidget(begin_date_choose,0,1)
		self.layout.addWidget(self.end_date,1,0)
		self.layout.addWidget(end_date_choose,1,1)
		self.layout.addWidget(close_bnt,2,0)
		self.layout.addWidget(query_bnt,2,1)

		self.layout.addWidget(self.txt,3,0,1,2)

		self.setLayout(self.layout)

	def query(self):
		db = DB()
		con = db.con
		cur = db.cur
		year = datetime.datetime.today().year

		sql = 'select * from zhiban where duty_date between %d and %d'%(self.change_date(self.begin_date), self.change_date(self.end_date))
		cur.execute(sql)
		rslt = cur.fetchall()
		dic = {}
		for i in rslt:
			name = i[1]
			tmp = self.to_pydate(i[0])
			month = tmp[1]
			day = tmp[2]
			if name not in dic.keys():
				dic2 = {}
				if month not in dic2.keys():
					dic2[month] = [day]
				else:
					dic2[month].append(day)
				dic[name] = dic2
			else:
				if month not in dic[name].keys():
					dic[name][month] = [day]
				else:
					dic[name][month].append(day)
		txt = str(dic).replace('},', '\n').replace('{', "").replace("'", "").replace('}',"")
		self.txt.setPlainText(txt)

	def begin_date_cal(self):
		self.cal = Calendar()
		self.cal.show()
		self.cal.date_signal.connect(self.begin_date_insert)

	def begin_date_insert(self, date):
		self.begin_date.setDate(date)

	def end_date_cal(self):
		self.cal = Calendar()
		self.cal.show()
		self.cal.date_signal.connect(self.end_date_insert)

	def end_date_insert(self, date):
		self.end_date.setDate(date)


	def to_pydate(self,a): #时间戳生成 [yyyy, mm, dd]列表
		b = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=a)
		return [b.year, b.month, b.day]

	def change_date(self,pyqtdate): 
		pydate = pyqtdate.date().toPyDate()
		base_date = datetime.date(1970,1,1)
		day_delta = pydate - base_date
		days = day_delta.days
		seconds = days*24*3600
		return seconds

if __name__ == '__main__':
	app = QApplication(sys.argv)
	window = Regist()
	window.show()
	sys.exit(app.exec_())